<?php
/*
 * @Author: 哲学家乔布斯 <wuyangii@qq.com>
 * @Date: 2022-10-20 21:41:37
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2022-11-07 22:37:15
 * @Descripttion: 系统配置
 */

namespace app\api\controller\collection\v1_0;
use think\Db;

class Goods extends Common
{
    protected $noNeedRight = ['test'];
    /**
     * @Author: 哲学家乔布斯 <wuyangii@qq.com>
     * @brief: 
     * @return 
     * @Descripttion: 首页默认接口
     */    
    public function add(){
        $param = request()->param();
        // var_dump($_POST);die;
        $param['img'] = empty($param['img']) ? [] : urldecode($param['img']);
        $param['imgs'] = empty($param['imgs']) ? [] : urldecode($param['imgs']);
        $param['content'] = empty($param['content']) ? [] : urldecode($param['content']);
     
        // file_put_contents("log/".time().".txt", json_encode($param));
        // var_dump(json_encode([
        //     ['sku_title' => "sku标题" ,'money' => '100' ,'money_cost' => '10']
        // ]));die;
        $goods = (new \app\common\model\Goods())->where(['title' => $param['title'],'is_del' => 0])->find();
        if($goods){
            return self::errorResult('该商品已经存在');
        }
        // if($goods){
        //     return self::errorResult("batch字段值 已经存在请更换！！！");   
        // }
        Db::startTrans();
        try{
            $message = "订单创建失败,请重试";
            $process = true;
           
            $img = $this->img($param['img']);
           
            if($img['state'] != 1){
                // $message = "商品图片上传失败,请重试！".$img['msg']."---".$param['img'];
                // $process = false;
                // return $message;
                $img['url'] = '';
            }

            $imgs = json_decode($param['imgs'],true);
          
           
            $imgs_arr = [];
            $imgs_val = "";
            foreach ($imgs as $key => $value) {
                if(!empty($value)){
                    $imgs_val = $this->img($value); 
                    if($imgs_val['state'] != 1){
                        // $message = "商品图片上传失败,请重试！".$imgs_val['msg']."------".$value;
                        // $process = false;
                        // return $message;
                        $imgs_arr[] = '';
                    }else{
                        $imgs_arr[] = $imgs_val['url'];
                    }
                    
                }
                
            }
           
            $imgs_arr = json_encode($imgs_arr);
    
            $content = json_decode($param['content'],true);
          
            $content_arr = [];
            $content_val = "";
            foreach ($content as $key => $value) {
                if(!empty($value)){
                    $content_val = $this->img($value); 
                    if($content_val['state'] != 1){
                        // $message = "商品图片上传失败,请重试！".$content_val['msg'];
                        // $process = false;
                        // return $message;
                        $content_val[] = '';
                    }else{
                        $content_arr[] = $content_val['url'];
                    }
                   
                }
            }
          
            $content_arr = json_encode($content_arr);
          
            $row = (new \app\common\model\Goods())->insertGetId([
                'title' => $param['title'],
                'cate_id' => $param['cate_id'],
                'introduce' => $param['introduce'],
                'img' => $img['url'],
                'imgs' => $imgs_arr,
                'content' => $content_arr,
                'batch' => $param['batch'],
                'content_html' => $param['content_html'],
                'is_display' => empty($param['is_display']) ? 1 : $param['is_display'],
            ]);
            if(!$row){
                $message = "主商品创建失败,请重试";
                $process = false;
            }
            $sku_data = [];
            $param['sku_data'] = json_decode($param['sku_data'],true);
          
            foreach ($param['sku_data'] as $key => $value) {
                if(!empty($value)){
                    $sku_data[] = [
                        'sku_title' => $value['sku_title'],
                        'money' => $value['money'],
                        'money_cost' => $value['money_cost'],
                        'goods_id' => $row,
                        'batch' => $param['batch'],
                        'stock' => $value['stock']
                     ];
                }
                 
            }
            $row = (new \app\common\model\GoodsSku())->insertAll($sku_data);
            if(!$row){
                $message = "sku创建失败,请重试";
                $process = false;
            }
            if ($process !== false) {
                Db::commit(); // 提交事务
            } else {
                Db::rollback(); // 回滚事务
            }
         } catch (\Exception $e) {
            $process = false;
            $message = $e->getMessage();
            Db::rollback(); // 回滚事务
        }
        if ($process !== false) {
            return self::successResult("成功");
        } else {
            return self::errorResult($message);
        }
       
       
    }
    public function img($img){ 
       
        $data = (new \app\common\controller\Upload())
                ->COSUploadFile('images/' . date('Ymd') . '/' .date('YmdHis') . rand(1000, 9999).".png",$img);
        return $data;
  
       
    }
    
    

}
